﻿using System;
using System.Collections;
using System.Text;

using System.Xml;

using MediaPortal.GUI.Library;
using MediaPortal.Profile;

namespace MyLeech.Sites
{
  class NewzLeech : iSites
  {
    Settings mpSettings = new Settings(MediaPortal.Configuration.Config.GetFolder(MediaPortal.Configuration.Config.Dir.Config) + @"\MyLeech.xml");
    HTTP_Common HTTP_Common = new HTTP_Common();

    public string SiteName
    {
      get { return "NewzLeech"; }
    }
    public string NZB_Method
    {
      get { return "POST"; }
    }
    public string NZB_Address
    {
      get { return "http://www.newzleech.com/index.php?m=gen"; }
    }
    private string _Keyword = String.Empty;
    public string Keyword
    {
      get { return _Keyword; }
    }
    private string _REFERER = @"http://www.newzleech.com/index.php";
    public string REFERER
    {
      get { return _REFERER; }
    }
    //
    private string SEARCH_URL = @"http://www.newzleech.com/rss.php?n=80&s=";
    private ArrayList Item_ID = new ArrayList();
    private ArrayList Name = new ArrayList();
    private ArrayList Size = new ArrayList();

    public void Do_Search(int TYPE, GUIListControl lstItems)
    {
      lstItems.Clear(); //clear list
      mediaportalFunc Dialogs = new mediaportalFunc();
      XmlDocument xmlDoc = new XmlDocument();
      //SO BASIC, ITS RETARDED.
      GUIPropertyManager.SetProperty("#Status", "Please Enter Keyword(s) to Search..."); //set status
      this._Keyword = Dialogs.Keyboard();
      int START = 0;
      int END = 0;
      string DESCRIPTION = String.Empty;
      string SIZE = String.Empty;
      string LINK = String.Empty;
      int L_START = 0;

      try
      {
        switch (TYPE)
        {
          case 0:
            string[] Filter = this.HTTP_Common.Filters(this.mpSettings.GetValueAsString("#Filters", "auto_movie", "DVDRIP;DVDScreener;DVD"));
            string[] Auto_Remove = this.HTTP_Common.Filters(this.mpSettings.GetValueAsString("#Filters", "auto_remove", "dubbed;german;french;russian;spanish"));
            //########## FOR EACH FILTER, GET RESULTS ################
            for (int x = 0; x < Filter.Length; x++)
            {
              XmlTextReader xmlTextReader = new XmlTextReader(this.SEARCH_URL + System.Web.HttpUtility.HtmlEncode(this._Keyword + " " + Filter[x]));
              xmlDoc.Load(xmlTextReader);
              if ((xmlDoc.ChildNodes[1].Name == "rss") && (xmlDoc.ChildNodes[1].Attributes["version"].InnerText == "2.0"))
              {
                XmlNodeList nodeList = xmlDoc.SelectNodes("rss/channel/item");

                foreach (XmlNode nodeItem in nodeList)
                {
                  
                  DESCRIPTION = System.Web.HttpUtility.HtmlDecode(nodeItem["description"].InnerText.ToString());
                  START = ((DESCRIPTION.ToLower().IndexOf("size:")) + 5);
                  END = DESCRIPTION.ToLower().IndexOf("<br />", START);
                  if (START > -1 & END > -1) this.Size.Add(DESCRIPTION.Substring(START, (END - START)));
                  else this.Size.Add("UKN MB");
                  LINK = nodeItem["link"].InnerText.ToString();
                  L_START = ((LINK.IndexOf("p=")) + 2);
                  if(START > -1) this.Item_ID.Add(LINK.Substring(L_START));
                  this.Name.Add(this.HTTP_Common.Clean_Name(System.Web.HttpUtility.HtmlDecode(nodeItem["title"].InnerText.ToString())));
                  START = 0;
                  END = 0;
                  L_START = 0;
                  DESCRIPTION = String.Empty;
                  SIZE = String.Empty;
                  LINK = String.Empty;

                }
                if (this.Item_ID.Count > 0) { break; }
              }
            }
            //############ REMOVE FILTER #####################
            for (int i = this.Name.Count - 1; i >= 0; i--)
            {
              if (this.HTTP_Common.Remove_Auto_Filter((this.Name[i].ToString()), Auto_Remove))
              {
                this.Item_ID.RemoveAt(i);
                this.Name.RemoveAt(i);
                this.Size.RemoveAt(i);
              }
            }
            for (int i = 0; i < this.Item_ID.Count; i++)
            {
              Dialogs.AddItem(lstItems, this.Name[i].ToString(), this.Size[i].ToString(), this.Item_ID[i].ToString(), 1);
            }
            //###############################################

            break;
          case 1:
            XmlTextReader xmlTextReader2 = new XmlTextReader(this.SEARCH_URL + System.Web.HttpUtility.HtmlEncode(this._Keyword));
            xmlDoc.Load(xmlTextReader2);
            XmlNodeList nodeList2 = xmlDoc.SelectNodes("rss/channel/item");

            foreach (XmlNode nodeItem2 in nodeList2)
            {
              DESCRIPTION = System.Web.HttpUtility.HtmlDecode(nodeItem2["description"].InnerText.ToString());
              START = ((DESCRIPTION.IndexOf("Size:")) + 5);
              END = DESCRIPTION.IndexOf("<br />", START);
              if (START > -1 & END > -1) this.Size.Add(DESCRIPTION.Substring(START, (END - START)));
              else this.Size.Add("UKN MB");

              LINK = nodeItem2["link"].InnerText.ToString();
              L_START = ((LINK.IndexOf("p=")) + 2);
              if (START > -1) this.Item_ID.Add(LINK.Substring(L_START));
              this.Name.Add(this.HTTP_Common.Clean_Name(System.Web.HttpUtility.HtmlDecode(nodeItem2["title"].InnerText.ToString())));
              START = 0;
              END = 0;
              L_START = 0;
              DESCRIPTION = String.Empty;
              SIZE = String.Empty;
              LINK = String.Empty;
            }
            if (this.Item_ID.Count > 0)
            {
              for (int i = 0; i < this.Item_ID.Count; i++)
              {
                Dialogs.AddItem(lstItems, this.Name[i].ToString(), this.Size[i].ToString(), this.Item_ID[i].ToString(), 1);
              }
            }
            break;
        }
        GUIPropertyManager.SetProperty("#PageTitle", "MyLeech - NewzLeech Search Results"); //Set Default Title
        GUIPropertyManager.SetProperty("#Status", "Found " + this.Item_ID.Count + " Results For " + this._Keyword); //set status
      }
      catch (Exception ex)
      {
        Log.Info("Data: " + ex.Data);
        Log.Info("HelpLink: " + ex.HelpLink);
        Log.Info("InnerException: " + ex.InnerException);
        Log.Info("Message: " + ex.Message + SEARCH_URL);
        Log.Info("Source: " + ex.Source);
        Log.Info("StackTrace: " + ex.StackTrace);
        Log.Info("TargetSite: " + ex.TargetSite);
      }
    }
  }
}
